package com.example.bnuzzu.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bnuzzu.common.dto.*;
import com.example.bnuzzu.common.lang.Result;
import com.example.bnuzzu.entity.*;
import com.example.bnuzzu.mapper.UserinfoMapper;
import com.example.bnuzzu.service.*;
import com.example.bnuzzu.util.JwtUtils;
import com.example.bnuzzu.util.ShiroUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


@Slf4j
@RestController
public class AdministratorController {
    final static String LOCAL_PATH = "File/images/upload";
    @Autowired
    HouseinfoService houseinfoService;
    @Autowired
    HousedetailsService housedetailsService;
    @Autowired
    HouseimageService houseimageService;
    @Autowired
    UserinfoService userService;
    @Autowired
    UserimageService userimageService;
    @Autowired
    OrderinfoService orderinfoService;
    @Autowired
    private UserinfoMapper userMapper;
    @Autowired
    RoommateService roommateService;
    @Autowired
    JwtUtils jwtUtils;

    /**
     * 房屋添加/更新页面
     * @param
     * @param
     * @return Result
     */
    @RequiresAuthentication
    @PostMapping("/administrators/edithouse")
    public Result editHouse(@RequestBody HouseDto houseDto, HttpServletResponse response){
        int userid = ShiroUtil.getProfile().getUserid().intValue();
        int usertype = userMapper.selectById(userid).getUsertype();
        if(usertype != 1){
            return Result.fail(555, "该用户没有权限", null);
        }
        //房屋信息
        Houseinfo houseinfo = new Houseinfo();
        BeanUtil.copyProperties(houseDto,houseinfo);
        System.out.println(houseinfo.getHouseid());
        //房屋详情
        Housedetails housedetails = new Housedetails();
        BeanUtil.copyProperties(houseDto,housedetails);
        //新建
        if(houseinfo.getHouseid() == null){
            houseinfoService.saveOrUpdate(houseinfo);
            housedetails.setHouseid(houseinfo.getHouseid());
            housedetailsService.saveOrUpdate(housedetails);
        }
        //更新
        else{
            houseinfoService.saveOrUpdate(houseinfo);
            housedetailsService.saveOrUpdate(housedetails);
        }
        return  Result.success(null);
    }

    @RequiresAuthentication
    @GetMapping(value = "/administrators/getuser/{id}")
    public Result getUser(@PathVariable(name = "id") Integer id){
        //查看用户权限
        int userid = ShiroUtil.getProfile().getUserid().intValue();
        int usertype = userMapper.selectById(userid).getUsertype();
        if(usertype != 1){
            return Result.fail(555, "该用户没有权限", null);
        }
        //用户信息
        Userinfo userinfo = userService.getById(id);
        Userimage userimage = userimageService.getById(id);
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("userinfo",userinfo);
        map.put("userimage",userimage);
        return  Result.success(map);
    }

    @RequiresAuthentication
    @PostMapping(value = "/administrators/edituser/{image}")
    public Result editUser(@RequestBody Userinfo userDto,@PathVariable(name = "image")String image){
        //查看用户权限
        int userid = ShiroUtil.getProfile().getUserid().intValue();
        int usertype = userMapper.selectById(userid).getUsertype();
        if(usertype != 1){
            return Result.fail(555, "该用户没有权限", null);
        }
        Userimage userimage = new Userimage();
        userimage.setUserimage(image);
        userimage.setUserid(userDto.getUserid());
        //用户信息
        userService.saveOrUpdate(userDto);
        userimageService.saveOrUpdate(userimage);
        return  Result.success(null);
    }

    @RequiresAuthentication
    @PostMapping(value = "/administrators/changeStatus/{id}")
    public Result editUser(@PathVariable(name = "id")Integer id){
        //查看用户权限
        int userid = ShiroUtil.getProfile().getUserid().intValue();
        int usertype = userMapper.selectById(userid).getUsertype();
        if(usertype != 1){
            return Result.fail(555, "该用户没有权限", null);
        }
        if(userService.getById(id).getUsertype() == 1) {
            return Result.fail(555, "管理员无法冻结", null);
        }
        Userinfo userinfo = userService.getById(id);
        int status = userinfo.getState();
        if(status == 1){
            userinfo.setState(0);
            userService.saveOrUpdate(userinfo);
        }else {
            userinfo.setState(1);
            userService.saveOrUpdate(userinfo);
        }
        return  Result.success(null);
    }

    @RequiresAuthentication
    @PostMapping(value = "/administrators/deleteuser")
    public Result deleteUser(@RequestBody Userinfo userDto){
        //查看用户权限
        int userid = ShiroUtil.getProfile().getUserid().intValue();
        int usertype = userMapper.selectById(userid).getUsertype();
        if(usertype != 1){
            return Result.fail(555, "该用户没有权限", null);
        }
        userDto.setState(1);
        userService.updateById(userDto);
        return Result.success(null);
    }

    @RequiresAuthentication
    @PostMapping(value = "/administrators/deletehouse")
    public Result deleteHouse(@RequestBody HouseDto houseDto){
        //查看用户权限
        int userid = ShiroUtil.getProfile().getUserid().intValue();
        int usertype = userMapper.selectById(userid).getUsertype();
        if(usertype != 1){
            return Result.fail(555, "该用户没有权限", null);
        }
        houseinfoService.removeById(houseDto.getHouseid());
        housedetailsService.removeById(houseDto.getHouseid());
        houseimageService.removeById(houseDto.getHouseid());

        return Result.success(null);
    }

    @RequiresAuthentication
    @GetMapping("/administrators/searchuser")
    public Result ulist(){
        int userid = ShiroUtil.getProfile().getUserid().intValue();
        int usertype = userMapper.selectById(userid).getUsertype();
        if(usertype != 1){
            return Result.fail(555, "该用户没有权限", null);
        }

        List<Userinfo> list = userService.getAllUser();
        return Result.success(list);
    }

    @RequiresAuthentication
    @GetMapping("/administrators/searchhouse")
    public Result hlist(){
        int userid = ShiroUtil.getProfile().getUserid().intValue();
        int usertype = userMapper.selectById(userid).getUsertype();
        if(usertype != 1){
            return Result.fail(555, "该用户没有权限", null);
        }

        List<HouseListDto> list = houseinfoService.getAllhouse();
        return Result.success(list);
    }

    @RequiresAuthentication
    @GetMapping("/administrators/searchmate")
    public Result mlist(){
        int userid = ShiroUtil.getProfile().getUserid().intValue();
        int usertype = userMapper.selectById(userid).getUsertype();
        if(usertype != 1){
            return Result.fail(555, "该用户没有权限", null);
        }

        List<RoommateListDto> list = roommateService.getAllMate();
        return Result.success(list);
    }

    @RequiresAuthentication
    @GetMapping("/administrators/searchorder")
    public Result olist(){
        int userid = ShiroUtil.getProfile().getUserid().intValue();
        int usertype = userMapper.selectById(userid).getUsertype();
        if(usertype != 1){
            return Result.fail(555, "该用户没有权限", null);
        }

        List<OrderListDto> list = orderinfoService.getAllOrder();
        return Result.success(list);
    }

    /**
     * 房屋添加/更新页面
     * @param orderinfo 房屋数据
     * @return Result 结果
     */
    @RequiresAuthentication
    @PostMapping("/administrators/editorder")
    public Result editInfo(@RequestBody Orderinfo orderinfo, HttpServletResponse response){
        //判断是否修改自己名下的订单
        int userid = ShiroUtil.getProfile().getUserid().intValue();
        int usertype = userMapper.selectById(userid).getUsertype();
        if(usertype != 1){
            return Result.fail(555, "该用户没有权限", null);
        }

        orderinfoService.saveOrUpdate(orderinfo);

        return  Result.success(null);
    }

    @RequiresAuthentication
    @PostMapping("/administrators/editmate")
    public Result editMate(@RequestBody Roommate roommate, HttpServletResponse response){
        int userid = ShiroUtil.getProfile().getUserid().intValue();
        int usertype = userMapper.selectById(userid).getUsertype();
        if(usertype != 1){
            return Result.fail(555, "该用户没有权限", null);
        }

        roommateService.updateById(roommate);

        return  Result.success(null);
    }

    @RequiresAuthentication
    @GetMapping (value = "/administrators/deletemate/{id}")
    public Result deleteMate(@PathVariable(name = "id")Integer id){
        int userid = ShiroUtil.getProfile().getUserid().intValue();
        int usertype = userMapper.selectById(userid).getUsertype();
        if(usertype != 1){
            return Result.fail(555, "该用户没有权限", null);
        }
        roommateService.removeById(id);
        return Result.success(null);
    }

}
